package com.framework.net;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.framework.R;

import android.app.Activity;
import android.net.LocalServerSocket;
import android.net.LocalSocket;
import android.net.LocalSocketAddress;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class ActivityNet extends Activity {
	SendTCPSocket mSendTCPSocket;
	private LocalSocket socket;
	protected String TAG= this.getClass().getName();
	private LocalSocketAddress endpoint;
	private LocalServerSocket mServerSocket;
	private boolean localSocketRunFlg = false;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_net_root);
		mSendTCPSocket = new SendTCPSocket();
		socket = new LocalSocket();
		try {

			mServerSocket = new LocalServerSocket("com.seraph.td");
			endpoint = new LocalSocketAddress("com.seraph.td");
			socket.setSendBufferSize(65534);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}
	public void simpleSendByte(View view){
		mSendTCPSocket.simpleSendByte(new String("=========hello android socket==========").getBytes());
	}
	public void getMethod(View view){
		new Thread(){

			@Override
			public void run() {
				// TODO Auto-generated method stub
				String result =null ;
				result = getHTMLContext("http://www.baidu.com");
				Log.d(TAG,"result===" +result);
			}
			
		}.start();
	}
	public void localSocket(View view){
		if(!localSocketRunFlg){
			localSocketRunFlg = true;
			new Thread(reveriveThread).start();
		}
		OutputStream out ;
		try {
			socket.connect(endpoint);
		out = socket.getOutputStream();
		out.write(new String("===HELLO SOCKET====").getBytes());
		out.flush();
		out.close();
		socket.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	Runnable reveriveThread = new Runnable() {
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
				try {
					byte[] buffer = new byte[1024]; 
					LocalSocket socket = mServerSocket.accept();
					InputStream  in = socket.getInputStream();
					int len = in.read(buffer);
					String str = new String(buffer,0,len);
					Log.d("com.seraph.td.socket",str);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
	};
	String getHTMLContext(String url){
		String result = null;
		HttpClient client = new DefaultHttpClient();
		HttpGet  method = new HttpGet(url);
		HttpResponse response = null;
		try {
		response =	client.execute(method);
		InputStream in = response.getEntity().getContent();
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		byte[] temp = new byte[1024];
		int len = in.read(temp);
		while(len != -1){
			out.write(temp, 0, len);
			len = in.read(temp);
		}
		result = new String(out.toByteArray());
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		return result;
	}
}
